/*
 * Copyright 2010 Electronic Business Systems Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * http://www.apache.org/licenses/LICENSE-2.0
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package gr.ebs.gssdroid.resource;
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.util.Log;

/**
 * @author kman
 */
public class SearchResource extends RestResource {

	public SearchResource(String aUri) {
		super(aUri);
	}
	String query;
	List<FileResource> files = new ArrayList<FileResource>();
	List<String> filePaths = new LinkedList<String>();

	public String getQuery() {
		return query;
	}
	public void setQuery(String query) {
		this.query = query;
	}
	/**
	 * Retrieve the files.
	 *
	 * @return the files
	 */
	public List<String> getFilePaths() {
		return filePaths;
	}

	/**
	 * Modify the files.
	 *
	 * @param newFilePaths the files to set
	 */
	public void setFilePaths(List<String> newFilePaths) {
		filePaths = newFilePaths;
	}

	/**
	 * Retrieve the files.
	 *
	 * @return the files
	 */
	public List<FileResource> getFiles() {
		return files;
	}

	/**
	 * Modify the files.
	 *
	 * @param newFiles the files to set
	 */
	public void setFiles(List<FileResource> newFiles) {
		files = newFiles;
	}

	@Override
	public void createFromJSON(String text) throws JSONException {
		Log.d("SEARCH",text);
		JSONArray subs = new JSONArray(text);
		if (subs != null)
			for (int i = 0; i < subs.length(); i++) {
				JSONObject fo = subs.getJSONObject(i);
				if (fo != null) {
					String fname = unmarshallString(fo, "name");
					String fowner = unmarshallString(fo, "owner");
					String fcontent = unmarshallString(fo, "content");
					String fpath = unmarshallString(fo, "path");
					fpath = URLDecoder.decode(fpath);
					Integer fversion = null;
					String versionString = unmarshallString(fo, "version");
					if (versionString != null)
						fversion = new Integer(versionString);
					boolean fdeleted = unmarshallBoolean(fo, "deleted");
					String testString = unmarshallString(fo, "creationDate");
					Date fcreationDate = null;
					Date fmodificationDate = null;
					if (testString != null)
						fcreationDate = new Date(new Long(testString));
					testString = unmarshallString(fo, "modificationDate");
					if (testString != null)
						fmodificationDate = new Date(new Long(testString));
					String furi = unmarshallString(fo, "uri");
					Long fsize = 0L;
					testString = unmarshallString(fo, "size");
					if (testString != null)
						fsize = new Long(testString);
					filePaths.add(furi);
					FileResource fs = new FileResource(furi);
					fs.setName(fname);
					fs.setOwner(fowner);
					fs.setPath(fpath);
					fs.setVersion(fversion);
					fs.setContentLength(fsize);
					fs.setDeleted(fdeleted);
					fs.setCreationDate(fcreationDate);
					fs.setModificationDate(fmodificationDate);
					fs.setContentType(fcontent);
					files.add(fs);
				}
			}
	}

	@Override
	public String getLastModifiedSince() {
		return null;
	}
}